package in.muton.edushare.panels.assignment;

import in.muton.edushare.pages.common.EdusharePanel;
import in.muton.edushare.url.EduShareURLPanel;
import in.muton.edushare.url.URLLoader;
import in.muton.edushare.wicket.models.CayenneModel;
import in.muton.panels.paginator.EdusharePageNavigator;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.apache.wicket.MarkupContainer;
import org.apache.wicket.PageParameters;
import org.apache.wicket.behavior.AttributeAppender;
import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.markup.html.CSSPackageResource;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Check;
import org.apache.wicket.markup.html.form.CheckGroup;
import org.apache.wicket.markup.html.form.CheckGroupSelector;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PageableListView;
import org.apache.wicket.markup.html.panel.EmptyPanel;
import org.apache.wicket.markup.html.panel.Fragment;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;

import com.edushare.common.constants.ApplicationConstants;
import com.edushare.common.constants.URLConstants;
import com.edushare.common.dao.AssignmentsDAO;
import com.edushare.common.dao.UserAssignmentLinkDAO;
import com.edushare.common.vo.Assignments;
import com.edushare.common.vo.Courses;
import com.edushare.common.vo.UserAssignmentLink;

public class UserAssignmentListPanel extends EdusharePanel {

	private static final long serialVersionUID = 887186940218389419L;
	private LIST_TYPE showListType;
	
	public static enum LIST_TYPE{
		LIST_AS_ASSIGNEE,
		LIST_AS_ASSIGNER
	};

	public UserAssignmentListPanel(String id/*, IModel<Users> uModel*/, LIST_TYPE listType) {
		super(id/*, uModel*/);
//		this.userModel = uModel;
		this.showListType = listType;
		add(CSSPackageResource.getHeaderContribution(AssignmentListPanel.class, "assignment.css"));
		loadPanel();
	}
	
	
	private void loadPanel() {
		
		WebMarkupContainer asnListCont = new WebMarkupContainer("asnListCont");
		asnListCont.setOutputMarkupId(true);
		add(asnListCont);
		Form<Void> form = new Form<Void>("assnForm");
		asnListCont.add(form);
		final CheckGroup<Assignments> assnGroup = new CheckGroup<Assignments>("assnGroup", new ArrayList<Assignments>());
		Button deleteButton = 
			new Button("deleteSelected"){

				private static final long serialVersionUID = -8209310939557457432L;
	
				@Override
				public void onSubmit() {
					Collection<Assignments> selItems = assnGroup.getModelObject();
					Iterator<Assignments> it = selItems.iterator();
					while(it.hasNext()){
						Assignments assn = it.next();
						it.remove();
						new AssignmentsDAO(getORMInterface()).deleteAssignmentsCompletly(assn);
					}
					
					getORMInterface().commitAllChanges();
				}
			};
			
		if(showListType == LIST_TYPE.LIST_AS_ASSIGNEE){
			deleteButton.setVisible(false);
		}
		form.add(deleteButton);
		form.add(assnGroup);
		CheckGroupSelector selectAll = new CheckGroupSelector("selectAll", assnGroup);
		if(this.showListType == LIST_TYPE.LIST_AS_ASSIGNEE){
			selectAll.setVisible(false);
		}
		assnGroup.add(selectAll);
		
		final WebMarkupContainer noItemDiv = new WebMarkupContainer("noItemDiv");
		PageableListView<IModel<UserAssignmentLink>> asnList = 
			new PageableListView<IModel<UserAssignmentLink>>
				(
						"asnList", 
						new PropertyModel<List<IModel<UserAssignmentLink>>>(UserAssignmentListPanel.this, "assignmentList"), 
						ApplicationConstants.PAGE_SIZE
				) {

				private static final long serialVersionUID = 4893935510739184188L;

				@Override
				protected void populateItem(ListItem<IModel<UserAssignmentLink>> item) {
					if(UserAssignmentListPanel.this.showListType==LIST_TYPE.LIST_AS_ASSIGNEE){
						item.add(new RemarkFrag("remarkDiv", this, item.getModelObject()));
					}else{
						item.add(new EmptyPanel("remarkDiv"));
					}
					
					CayenneModel<Assignments> itemModel = 
						new CayenneModel<Assignments>(item.getModelObject().getObject().getAssignment());
					
					Check<Assignments> check = new Check<Assignments>("assnCheck", itemModel);
					if(UserAssignmentListPanel.this.showListType == LIST_TYPE.LIST_AS_ASSIGNEE){
						check.setVisible(false);
					}
					item.add(check);
					PageParameters asnPageParams = new PageParameters();
					asnPageParams.add("0", ""+itemModel.getObject().getAssignmentId());
					BookmarkablePageLink<Void> assnPageLink = 
						new BookmarkablePageLink<Void>("assnPageLink", URLLoader.getPageClass(URLConstants.ASSIGNMENT_PATH), asnPageParams);
					assnPageLink.add(new Label("assnName", new PropertyModel<String>(itemModel.getObject(), "assignmentTitle")).setRenderBodyOnly(true));
					item.add(assnPageLink);
					
					EduShareURLPanel courseURLPanel = new EduShareURLPanel
														("assnCourseLink", 
														 new CayenneModel<Courses>(itemModel.getObject()
																 					.getCourseInstance()
																 					.getCourse())
														);
					item.add(courseURLPanel);
					
					item.add(new Label("assnStartDate", new PropertyModel<String>(itemModel.getObject(), "formattedStartDate")));
					item.add(new Label("assnEndDate", new PropertyModel<String>(itemModel.getObject(), "formattedEndDate")));
					item.add(new Label("assnStatus", new PropertyModel<String>(itemModel.getObject(), "statusAsString")));
				}
				
				@Override
					protected void onBeforeRender() {
					if(this.getList()==null || this.getList().size()<1){
						noItemDiv.setVisible(true);
					}else{
						noItemDiv.setVisible(false);
					}
					super.onBeforeRender();
				}
				
			};
		assnGroup.add(asnList);	
		assnGroup.add(noItemDiv);
		assnGroup.add(new EdusharePageNavigator("asnPagination", asnList));
	}
	
	public List<IModel<UserAssignmentLink>> getAssignmentList(){
		
		List<UserAssignmentLink> al = null;
		if(this.showListType == LIST_TYPE.LIST_AS_ASSIGNEE){
			al = new UserAssignmentLinkDAO(getORMInterface()).getAssignments(getLoggedInUser(), UserAssignmentLink.ASSIGNEE);
			
		}else{
			al = new UserAssignmentLinkDAO(getORMInterface()).getAssignments(getLoggedInUser(), UserAssignmentLink.ASSIGNER);
		}
		ArrayList<IModel<UserAssignmentLink>> ml = new ArrayList<IModel<UserAssignmentLink>>(); 
		for(UserAssignmentLink a: al){
			ml.add(new CayenneModel<UserAssignmentLink>(a));
		}
		
		
		return ml;
	}
	
	public class RemarkFrag extends Fragment{

		public RemarkFrag(String id, MarkupContainer container, IModel<UserAssignmentLink> model) {
			super(id, "remarkFrag", container, model);
			Label status = new Label("status","");
			add(status);
			if(model.getObject().getEvaluationStat()==UserAssignmentLink.EVALUATED){
				status.add(new AttributeAppender("class", true, new Model<String>("ui-icon-check"), " "));
				status.add(new SimpleAttributeModifier("title", "Evaluated"));
			}
			else{
				status.add(new AttributeAppender("class", true, new Model<String>("ui-icon-notice"), " "));
				status.add(new SimpleAttributeModifier("title", "Waiting Evaluation"));
			}
			
			add(new Label("remarkTxt", model.getObject().getRemarks()));
		}
		
	}

}
